#!/bin/sh -e

# ----------------------------------------------------------------------
# check-refs
# Check for broken refs in Markdown files
# ----------------------------------------------------------------------

if ! test $# -eq 1
then
  echo 'usage: check-refs file' 1>&2
  exit 1
fi

./defined-tags < $1 > defined-tags.txt
./used-tags < $1 > used-tags.txt
undefined_tags=`./undefined-tags defined-tags.txt used-tags.txt`
if test -n "$undefined_tags"
then
  echo "WARNING: References use undefined tags" 1>&2
  echo $undefined_tags | tr ' ' '\n'
fi

bad_refs=`awk -f bad-refs.awk $1 | tr '\n' ',' | sed s'/,$//'`
if test -n "$bad_refs"
then
  echo "WARNING: Malformed references"
  echo $bad_refs | tr ',' '\n'
fi
